import { mapState } from 'vuex'

export const jumpMixin = {
  data() {
    return {
      dataList: []
    }
  },
  computed: {
    ...mapState({
      menus: (state) => state.user.menus,
    }),
  },
  mounted() {
    console.log(this.menus, '11111111111')
    this.generateList(this.menus)
  },
  methods: {
    // 把多级菜单生成单级全部列表
    generateList(data) {
      for (let i = 0; i < data.length; i++) {
        const node = data[i]
        const id = node.id
        this.dataList.push({ id, parentId: node.parentId, value: node.raw.value, name: node.raw.name, code: node.raw.code })
        if (node.children) {
          this.generateList(node.children)
        }
      }
    },
    //显示是否跳转
    showJump(record) {
      let arr = ['施组方案上报通知','施组方案交底通知', '双优化未实施情况'],
        text = record?.text?.title || record?.title,
        flag = false;
      flag = arr.includes(text);
      return flag;
    },
    //url
    getJump(record) {
      let str = window.location.origin;
      let title = record?.text?.title || record?.title;

      if (title == '施组方案上报通知') {
        str = `${str}/technical/ShiGroup/Approval?query=prma_seex`;
      }else if(title == '施组方案交底通知'){
        str = `${str}/technical/constructionSchemeMeeting/constructionSchemeMeetingList?query=prma_seex`;
      }
      else if(title == '双优化未实施情况'){
        str = `${str}/technical/monthlyReport/OptimizedConditions?query=prma_seex`;
      }
      return str;
    },
    getActiveCodes(currentId) {
      const current = this.dataList.find(item => currentId === item.id)
      if (current) {
        this.activeCodes.unshift(current.code)
        if (current.parentId) {
          this.getActiveCodes(current.parentId)
        }
      }
    },
    // 设置激活菜单
    setActivePath(path) {
      const current = this.dataList.find(item => (
        item.value === path &&
        item.code.indexOf('seex') > -1
      ))
      if(current) {
        this.activeCodes = []
        this.getActiveCodes(current.id)
        this.$store.commit('SET_ACTIVE_CODES', this.activeCodes)
        return true
      } else {
        this.$message.warning('权限不足，请联系管理员添加菜单')
        return false
      }

    },
    //跳转
    clickJump(record) {
      let title = record?.text?.title || record?.title;
      if (title == '施组方案上报通知') {
        this.setActivePath('/technical/ShiGroup/Approval')
        &&
        this.$router.push({
          path: '/technical/ShiGroup/Approval?query=prma_seex',
        })
      }else if(title == '施组方案交底通知'){
        this.setActivePath('/technical/constructionSchemeMeeting/constructionSchemeMeetingList')
        &&
        this.$router.push({
          path: '/technical/constructionSchemeMeeting/constructionSchemeMeetingList?query=prma_seex',
        })
      } else if(title == '双优化未实施情况'){
        this.setActivePath('/technical/monthlyReport/OptimizedConditions')
        &&
        this.$router.push({
          path: '/technical/monthlyReport/OptimizedConditions?query=prma_seex',
        })
      }
    },

  }
}